package org.jeecg.modules.demo.mall.yxstorecoupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.SneakyThrows;
import org.jeecg.modules.demo.mall.entity.YxStoreCoupon;
import org.jeecg.modules.demo.mall.entity.YxStoreCouponUser;
import org.jeecg.modules.demo.mall.yxstorecoupon.mapper.YxStoreCouponMapper;
import org.jeecg.modules.demo.mall.yxstorecoupon.service.IYxStoreCouponService;
import org.jeecg.modules.demo.mall.yxstorecouponuser.service.IYxStoreCouponUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 优惠券表
 * @Author: jeecg-boot
 * @Date: 2023-11-03
 * @Version: V1.0
 */
@Service
public class YxStoreCouponServiceImpl extends ServiceImpl<YxStoreCouponMapper, YxStoreCoupon> implements IYxStoreCouponService {

    @Autowired
    private IYxStoreCouponUserService yxStoreCouponUserService;

    @SneakyThrows
    @Override
    public YxStoreCoupon saveTime(YxStoreCoupon yxStoreCoupon) {

        if (StringUtils.hasText(yxStoreCoupon.getStartT()) && StringUtils.hasText(yxStoreCoupon.getEndT())) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            yxStoreCoupon.setStartTime(sdf.parse(yxStoreCoupon.getStartT()));
            yxStoreCoupon.setEndTime(sdf.parse(yxStoreCoupon.getEndT()));
        }
        return yxStoreCoupon;
    }

    /**
     * 获取用户优惠券
     *
     * @param merId
     * @param uid
     * @return
     */
    @Override
    public List<YxStoreCouponUser> queryUserCoupon(String merId, Integer uid) {

        //判断符合门店的优惠券
        LambdaQueryWrapper<YxStoreCoupon> couponQueryWrapper = new LambdaQueryWrapper<>();
        couponQueryWrapper.eq(YxStoreCoupon::getStatus, 1);
//        couponQueryWrapper.;
        //无门店要求、本门店可用
        couponQueryWrapper.and(queryWrapper -> queryWrapper.like(YxStoreCoupon::getMerIds, merId).or().eq(YxStoreCoupon::getIsMer, 0));
        //判断没有过期的优惠券
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        couponQueryWrapper.and(queryWrapper3 -> queryWrapper3.apply("end_time >= '" + sdf.format(new Date()) + "'").or().eq(YxStoreCoupon::getIsFixedDate, 0));
        List<YxStoreCoupon> yxStoreCouponList = this.list(couponQueryWrapper);
        if (CollectionUtils.isEmpty(yxStoreCouponList)) {
            return null;
        }
        List<Integer> couponInteger = yxStoreCouponList.stream().map(YxStoreCoupon::getId).collect(Collectors.toList());
        //查询用户优惠券。
        LambdaQueryWrapper<YxStoreCouponUser> yxStoreCouponUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
        yxStoreCouponUserLambdaQueryWrapper.in(YxStoreCouponUser::getCid, couponInteger);
        yxStoreCouponUserLambdaQueryWrapper.eq(YxStoreCouponUser::getUid, uid);
//       查询所有不能用的优惠券   yxStoreCouponUserLambdaQueryWrapper.and(queryWrapper -> queryWrapper.apply("end_time <= '" + sdf.format(new Date()) + "'").or().apply("end_time IS NOT NULL"));
        yxStoreCouponUserLambdaQueryWrapper.eq(YxStoreCouponUser::getStatus, 0);
        List<YxStoreCouponUser> users = yxStoreCouponUserService.list(yxStoreCouponUserLambdaQueryWrapper);
        //过期的修改
        LambdaQueryWrapper<YxStoreCouponUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(YxStoreCouponUser::getUid, uid);
        lambdaQueryWrapper.isNotNull(YxStoreCouponUser::getEndTime);
        //过期的修改
        List<YxStoreCouponUser> yxStoreCouponUsers = yxStoreCouponUserService.list(lambdaQueryWrapper);
        if (!CollectionUtils.isEmpty(yxStoreCouponUsers)) {
            yxStoreCouponUsers.forEach(yxStoreCouponUser -> {
                if (yxStoreCouponUser.getEndTime().compareTo(new Date()) < 0) {
                    yxStoreCouponUser.setStatus(2);
                }
            });
            yxStoreCouponUserService.updateBatchById(yxStoreCouponUsers);
        }

        return users;
    }

    @Override
    public List<YxStoreCouponUser> updateCoupon(YxStoreCouponUser req, List<YxStoreCouponUser> yxStoreCouponUserList) {
        //可以使用  不可以使用
        List<YxStoreCouponUser> isSort = new ArrayList<>();
        //不可以使用
        List<YxStoreCouponUser> isNotSort = new ArrayList<>();


        List<YxStoreCouponUser> userList = yxStoreCouponUserList.stream().map(item1 -> {
            //判断类型是否符合条件
            if (req.getCouponType() != null) {
                if (!item1.getCouponType().equals(req.getCouponType())) {
                    item1.setIsUsage(1);
                    isNotSort.add(item1);
                    return item1;
                }
            }
            //判断金额是否符合条件
            if (req.getUseMinPrice() != null && req.getUseMinPrice().compareTo(item1.getUseMinPrice()) < 0) {
                item1.setIsUsage(1);
                isNotSort.add(item1);
                return item1;
            }
            item1.setIsUsage(0);
            isSort.add(item1);
            return item1;
        }).collect(Collectors.toList());
        isSort.addAll(isNotSort);
        return isSort;
    }


    @Override
    public void updateExpireDate(YxStoreCouponUser req) {
        //过期的数据进行修改
        LambdaQueryWrapper<YxStoreCouponUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(YxStoreCouponUser::getUid, req.getUid());
        lambdaQueryWrapper.isNotNull(YxStoreCouponUser::getEndTime);
        //过期的修改
        List<YxStoreCouponUser> yxStoreCouponUsers = yxStoreCouponUserService.list(lambdaQueryWrapper);
        if (!CollectionUtils.isEmpty(yxStoreCouponUsers)) {
            yxStoreCouponUsers.forEach(yxStoreCouponUser -> {
                if (yxStoreCouponUser.getEndTime().compareTo(new Date()) < 0) {
                    yxStoreCouponUser.setStatus(2);
                }
            });
            yxStoreCouponUserService.updateBatchById(yxStoreCouponUsers);
        }
    }

    /**
     * 获取用户其他优惠券不可用的
     *
     * @param req
     * @return
     */
    @Override
    public List<YxStoreCouponUser> queryIsNotCoupon(YxStoreCouponUser req) {

        //判断符合门店的优惠券
        LambdaQueryWrapper<YxStoreCoupon> couponQueryWrapper = new LambdaQueryWrapper<>();
        couponQueryWrapper.eq(YxStoreCoupon::getStatus, 1);
//        couponQueryWrapper.;
        //无门店要求、本门店可用
        couponQueryWrapper.and(queryWrapper -> queryWrapper.like(YxStoreCoupon::getMerIds, req.getMerId()).or().eq(YxStoreCoupon::getIsMer, 0));
        //哦按段不符合商品的数据
        couponQueryWrapper.and(queryWrapper -> queryWrapper.notLike(YxStoreCoupon::getProductId, req.getProductId()).ne(YxStoreCoupon::getIsProduct, 0).eq(YxStoreCoupon::getMerType, req.getMerType()).or().ne(YxStoreCoupon::getMerType, req.getMerType()));
        //判断没有过期的优惠券
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        couponQueryWrapper.and(queryWrapper3 -> queryWrapper3.apply("end_time >= '" + sdf.format(new Date()) + "'").or().eq(YxStoreCoupon::getIsFixedDate, 0));
        List<YxStoreCoupon> yxStoreCouponList = this.list(couponQueryWrapper);
        if (CollectionUtils.isEmpty(yxStoreCouponList)) {
            return null;
        }
        List<Integer> couponInteger = yxStoreCouponList.stream().map(YxStoreCoupon::getId).collect(Collectors.toList());
        //查询用户优惠券。
        LambdaQueryWrapper<YxStoreCouponUser> yxStoreCouponUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
        yxStoreCouponUserLambdaQueryWrapper.in(YxStoreCouponUser::getCid, couponInteger);
        yxStoreCouponUserLambdaQueryWrapper.eq(YxStoreCouponUser::getUid, req.getUid());
//       查询所有不能用的优惠券   yxStoreCouponUserLambdaQueryWrapper.and(queryWrapper -> queryWrapper.apply("end_time <= '" + sdf.format(new Date()) + "'").or().apply("end_time IS NOT NULL"));
        yxStoreCouponUserLambdaQueryWrapper.eq(YxStoreCouponUser::getStatus, 0);
        List<YxStoreCouponUser> users = yxStoreCouponUserService.list(yxStoreCouponUserLambdaQueryWrapper);
        if (CollectionUtils.isEmpty(users)) {
            return users;
        }
        users.forEach(yxStoreCouponUser -> {
            yxStoreCouponUser.setIsUsage(1);
        });
        return users;
    }

    /**
     * @param req
     * @return
     */
    @Override
    public List<YxStoreCouponUser> queryNot(YxStoreCouponUser req) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //判断符合门店的优惠券
        LambdaQueryWrapper<YxStoreCoupon> couponQueryWrapper = new LambdaQueryWrapper<>();
        //状态开启的优惠券
        couponQueryWrapper.eq(YxStoreCoupon::getStatus, 1);
        //无门店要求、本门店可用
        couponQueryWrapper.and(queryWrapper -> queryWrapper.like(YxStoreCoupon::getMerIds, req.getMerId()).or().eq(YxStoreCoupon::getIsMer, 0));
        //判断没有过期的优惠券
        couponQueryWrapper.and(queryWrapper3 -> queryWrapper3.apply("end_time >= '" + sdf.format(new Date()) + "'").or().eq(YxStoreCoupon::getIsFixedDate, 0));
        if (req.getIsProduct() != null) {
            couponQueryWrapper.ne(YxStoreCoupon::getIsProduct, req.getIsProduct());
        }
        List<YxStoreCoupon> yxStoreCouponList = this.list(couponQueryWrapper);

        if (CollectionUtils.isEmpty(yxStoreCouponList)) {
            return new ArrayList<YxStoreCouponUser>();
        }
        List<Integer> couponInteger = yxStoreCouponList.stream().map(YxStoreCoupon::getId).collect(Collectors.toList());
        //查询用户优惠券。
        LambdaQueryWrapper<YxStoreCouponUser> yxStoreCouponUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
        yxStoreCouponUserLambdaQueryWrapper.in(YxStoreCouponUser::getCid, couponInteger);
        yxStoreCouponUserLambdaQueryWrapper.eq(YxStoreCouponUser::getStatus, 0);
        yxStoreCouponUserLambdaQueryWrapper.eq(YxStoreCouponUser::getUid, req.getUid());
        List<YxStoreCouponUser> users = yxStoreCouponUserService.list(yxStoreCouponUserLambdaQueryWrapper);
        if (CollectionUtils.isEmpty(users)) {
            return new ArrayList<YxStoreCouponUser>();
        }
        users.forEach(yxStoreCouponUser -> {
            yxStoreCouponUser.setIsUsage(1);
        });
        return users;
    }
}
